import { baseURL } from "./apiurl";
import store from "../store";
import Config from "./Config";

export function request(url, params, method = 'POST') {
	return new Promise((resolve, reject) => {
		url = baseURL + url

		uni.request({
			url: url,
			data: {
				...params,
				sessionId: store.state.sid || ''
			},
			method: method,
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			success(data) {
				if (data.statusCode === 200) {
					resolve(data.data)
				} else {
					uni.showToast({
						title: '请求失败,请稍候重试',
						icon: 'none'
					})
					reject(data)
				}
			}
		})
	})
}

export default class ApiManage {
	doPost(url, params) {
		return this.request(url, params, 'POST')
	}

	doGet(url, params) {
		return this.request(url, params, 'GET')
	}

	request(url, params, method) {
		return new Promise((resolve, reject) => {

			url = Config.BASE_URL + url

			uni.request({
				url: url,
				data: {
					...params,
					sessionId: store.state.sid || ''
				},
				method: method,
				header: {
					"Content-Type": 'application/x-www-form-urlencoded'
				},
				success(data) {
					if (data.statusCode === 200) {
						resolve(data.data)
					} else {
						uni.showToast({
							title: '请求失败,请稍候重试',
							icon: 'none'
						})
						reject(data)
					}
				}
			})
		})
	}


}
